package com.itheima.controller;

import com.itheima.pojo.Result;
import com.itheima.util.AliOssUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class FileUploadController {
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws Exception {
        //把文件的内容储存到本地磁盘上
        String originalFilename= file.getOriginalFilename();
        //UUID 的使用
        String fileName= UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));


        //据说路径不能有中文    ,但实际操作中可以有
//        file.transferTo(new File("C:\\Users\\大神之路\\Desktop\\files\\"+fileName));

        String url= AliOssUtil.upLoadFile(fileName,file.getInputStream());
        return Result.success(url);

    }
}
